package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.annotaion.RepeatSubmit;
import com.xmy.cultivate.entity.PracticePlan;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.response.PracticeCheckRade;
import com.xmy.cultivate.entity.response.Reconciliation;
import com.xmy.cultivate.service.IPracticePlanService;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.ISchoolIntendantService;
import com.xmy.cultivate.service.ISchoolQuarterService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 练课进度 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/api/practicePlan")
@Scope("prototype")
public class PracticePlanController extends BaseController{

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    @Autowired
    @Lazy
    IPracticePlanService iPracticePlanService;

    @Autowired
    @Lazy
    ISchoolQuarterService iSchoolQuarterService;

    @Autowired
    @Lazy
    ISchoolIntendantService iSchoolIntendantService;

    @GetMapping("/getEnterList")
    public Result getEnterList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize",defaultValue = "-1") Integer pageSize,
                               @RequestParam(value = "yearPart",required = false) Integer yearPart,
                               @RequestParam(value = "quarterNum",required = false) Integer quarterNum,
                               @RequestParam(value = "subjectsId",required = false) String subjectsId,
                               @RequestParam(value = "yearClassId",required = false) String yearClassId,
                               @RequestParam(value = "lessonType",required = false,defaultValue = "1") Integer lessonType,
                               @RequestParam(value = "classTypeId",required = false) String classTypeId,
                               @RequestParam(value = "teacherName",required = false) String teacherName,
                               @RequestParam(value = "lessonNumStart",required = false) Integer lessonNumStart,
                               @RequestParam(value = "lessonNumEnd",required = false) Integer lessonNumEnd,
                               @RequestParam(value = "practiceTypeId",required = false) Long practiceTypeId){
        Page<PracticePlan> iPage = new Page<>(pageNum, pageSize);

        Quarter quarterNow = iQuarterService.getNowQuarter();
        if (yearPart == null || quarterNum == null) {
            yearPart = quarterNow.getYearPart();
            quarterNum = quarterNow.getNum();
        }

        String schoolId = "";
        if (this.adminId.equals(1568138350324350975L)){
            schoolId = this.baseSchoolIdStr;
        }else {
            //主管管理的管区id
            /*String inSchoolId = iSchoolIntendantService.getSchoolIdsForTeacherId(yearPart,quarterNum,this.adminId,1);
            if (StringUtils.isBlank(inSchoolId)){
                return new Result(Code.GET_OK, new PageResult(iPage.getRecords(),iPage.getTotal(), iPage.getPages()), "获取成功");
            }
            schoolId = iSchoolQuarterService.getChilIds(yearPart,quarterNum,inSchoolId);
            if (StringUtils.isBlank(schoolId)){
                return new Result(Code.GET_OK, new PageResult(iPage.getRecords(),iPage.getTotal(), iPage.getPages()), "获取成功");
            }*/
            schoolId = "0";
        }
        iPracticePlanService.getEnterList(iPage,yearPart,quarterNum,schoolId,lessonType,yearClassId,classTypeId,subjectsId,lessonNumStart,lessonNumEnd,practiceTypeId,teacherName,this.adminId);

        return new Result(Code.GET_OK, new PageResult(iPage.getRecords(),iPage.getTotal(), iPage.getPages()), "获取成功");
    }

    @GetMapping("/getEnterDetail")
    public Result getEnterDetail(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize",defaultValue = "-1") Integer pageSize,
                               @RequestParam(value = "yearPart",required = false) Integer yearPart,
                               @RequestParam(value = "quarterNum",required = false) Integer quarterNum,
                               @RequestParam(value = "subjectsId",required = false) String subjectsId,
                               @RequestParam(value = "yearClassId",required = false) String yearClassId,
                               @RequestParam(value = "lessonType",required = false,defaultValue = "1") Integer lessonType,
                               @RequestParam(value = "classTypeId",required = false) String classTypeId,
                               @RequestParam(value = "lessonNumStart",required = false) Integer lessonNumStart,
                               @RequestParam(value = "lessonNumEnd",required = false) Integer lessonNumEnd,
                               @RequestParam(value = "practiceTypeId",required = false) Long practiceTypeId,
                               @RequestParam(value = "teacherId",required = false) Long teacherId,
                               @RequestParam(value = "isFinish",required = false) String isFinish
                                 ){

        Quarter quarterNow = iQuarterService.getNowQuarter();
        if (yearPart == null || quarterNum == null) {
            yearPart = quarterNow.getYearPart();
            quarterNum = quarterNow.getNum();
        }

        Page<PracticePlan> iPage = new Page<>(pageNum, pageSize);
        String schoolId = "";
        if (this.adminId.equals(1568138350324350975L)){
            schoolId = this.baseSchoolIdStr;
        }else {
            //主管管理的管区id
            /*String inSchoolId = iSchoolIntendantService.getSchoolIdsForTeacherId(yearPart,quarterNum,this.adminId,1);
            if (StringUtils.isBlank(inSchoolId)){
                return new Result(Code.GET_OK, new PageResult(iPage.getRecords(),iPage.getTotal(), iPage.getPages()), "获取成功");
            }
            schoolId = iSchoolQuarterService.getChilIds(yearPart,quarterNum,inSchoolId);
            if (StringUtils.isBlank(schoolId)){
                return new Result(Code.GET_OK, new PageResult(iPage.getRecords(),iPage.getTotal(), iPage.getPages()), "获取成功");
            }*/
            schoolId = "0";
        }
        iPracticePlanService.getEnterDetail(iPage,yearPart,quarterNum,schoolId,lessonType,yearClassId,classTypeId,subjectsId,lessonNumStart,lessonNumEnd,practiceTypeId,teacherId,isFinish);

        return new Result(Code.GET_OK, new PageResult(iPage.getRecords(),iPage.getTotal(), iPage.getPages()), "获取成功");
    }

    @PostMapping("/enterSave")
    @RepeatSubmit(expireSeconds = 3)
    public Result enterSave(@RequestBody PracticePlan practicePlan){
        practicePlan.setCreatedId(this.adminId);
        Result rs = iPracticePlanService.enterSave(practicePlan);
        return rs;
    }

    @GetMapping("/getCheckList")
    public Result getCheckList(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                               @RequestParam(value = "yearPart",required = false) Integer yearPart,
                               @RequestParam(value = "quarterNum",required = false) Integer quarterNum,
                               @RequestParam(value = "subjectsId",required = false) String subjectsId,
                               @RequestParam(value = "yearClassId",required = false) String yearClassId,
                               @RequestParam(value = "classTypeId",required = false) String classTypeId,
                               @RequestParam(value = "lessonNumStart",required = false) Integer lessonNumStart,
                               @RequestParam(value = "lessonNumEnd",required = false) Integer lessonNumEnd,
                               @RequestParam(value = "practiceTypeId",required = false) Long practiceTypeId,
                               @RequestParam(value = "isCheck",required = false) String isCheck,
                               @RequestParam(value = "starDate",required = false) String starDate,
                               @RequestParam(value = "endDate",required = false) String endDate){
        Page<PracticePlan> iPage = new Page<>(pageNum, pageSize);
        iPracticePlanService.getCheckList(iPage,yearPart,quarterNum,yearClassId,classTypeId,subjectsId,lessonNumStart,lessonNumEnd,practiceTypeId,this.adminId,isCheck,starDate,endDate);

        return new Result(Code.GET_OK, new PageResult(iPage.getRecords(),iPage.getTotal(), iPage.getPages()), "获取成功");
    }

    @PostMapping("/batchCheck")
    public Result batchCheck(@RequestBody PracticePlan practicePlan){
        return iPracticePlanService.batchCheck(practicePlan,this.adminId);
    }

    @GetMapping("/checkRadeList")
    public Result checkRadeList(
                                @RequestParam(value = "yearPart",required = false) String yearPart,
                                @RequestParam(value = "quarterNum",required = false) String quarterNum,
                                @RequestParam(value = "subjectsId",required = false) String subjectsId,
                                @RequestParam(value = "yearClassId",required = false) String yearClassId,
                                @RequestParam(value = "classTypeId",required = false) String classTypeId,
                                @RequestParam(value = "lessonNumStart",required = false) String lessonNumStart,
                                @RequestParam(value = "lessonNumEnd",required = false) String lessonNumEnd,
                                @RequestParam(value = "practiceTypeId",required = false) String practiceTypeId,
                                @RequestParam(value = "starDate",required = false) String starDate,
                                @RequestParam(value = "endDate",required = false) String endDate,
                                @RequestParam(value = "type",required = false,defaultValue = "1") String type,
                                @RequestParam(value = "teacherName",required = false,defaultValue = "") String teacherName
                                ){
        List<PracticeCheckRade> practiceCheckRadeList = iPracticePlanService.checkRadeList(yearPart,quarterNum,lessonNumStart,lessonNumEnd,starDate,endDate,type,yearClassId,classTypeId,subjectsId,practiceTypeId,teacherName,"1","");
        return new Result(Code.GET_OK, new PageResult(practiceCheckRadeList,(long)practiceCheckRadeList.size(), 1L), "获取成功");
    }

    @GetMapping("/checkRadeDetail")
    public Result checkRadeDetail(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                  @RequestParam(value = "yearPart",required = false) String yearPart,
                                  @RequestParam(value = "quarterNum",required = false) String quarterNum,
                                  @RequestParam(value = "subjectsId",required = false) String subjectsId,
                                  @RequestParam(value = "yearClassId",required = false) String yearClassId,
                                  @RequestParam(value = "classTypeId",required = false) String classTypeId,
                                  @RequestParam(value = "lessonNumStart",required = false) String lessonNumStart,
                                  @RequestParam(value = "lessonNumEnd",required = false) String lessonNumEnd,
                                  @RequestParam(value = "practiceTypeId",required = false) String practiceTypeId,
                                  @RequestParam(value = "starDate",required = false) String starDate,
                                  @RequestParam(value = "endDate",required = false) String endDate,
                                  @RequestParam(value = "type",required = false,defaultValue = "1") String type,
                                  @RequestParam(value = "isCheck",required = false,defaultValue = "1") String isCheck,
                                  @RequestParam(value = "checkTeacherId",required = false,defaultValue = "") String checkTeacherId){
        Page<PracticePlan> iPage = new Page<>(pageNum, pageSize);
        iPracticePlanService.checkRadeDetail(iPage,yearPart,quarterNum,lessonNumStart,lessonNumEnd,starDate,endDate,type,yearClassId,classTypeId,subjectsId,practiceTypeId,checkTeacherId,"1","",isCheck);
        return new Result(Code.GET_OK, new PageResult(iPage.getRecords(),iPage.getTotal(), iPage.getPages()), "获取成功");
    }
}
